package com.hctforgreen.greenservice.machineManager.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.http.impl.conn.DefaultClientConnection;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.StartPwdActivity;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.machineManager.MachineBase;
import com.hctforgreen.greenservice.model.AttrEntity;
import com.hctforgreen.greenservice.model.JYHotWaterMachineEntity;
import com.hctforgreen.greenservice.model.LocationEntity;
import com.hctforgreen.greenservice.model.PwdEntity;
import com.hctforgreen.greenservice.ui.adapter.SpinnerAttrsAdapter;
import com.hctforgreen.greenservice.ui.adapter.SpinnerStringsAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.os.Parcel;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * 家用热水机
 * @author 390694
 *
 */
public class JY_HotWaterMachine extends MachineBase{
	private View layout_JY_HotWaterMachine;
	private View layout_MachineAttr;
	
	private Spinner spinner_MachineAttr;
	private Spinner spinner_water_tank_installation_position;
	private Spinner spinner_refrigerant_connecting_tube;
	private Spinner spinner_use_of_water;
	private Spinner spinner_number_of_people_use;
	private Spinner spinner_hot_water_point;
	private Spinner spinner_apartment_layout_area;
	private Spinner spinner_water_inlet_sequence;
	private View dialogTip ;

	public JYHotWaterMachineEntity jyHotWaterMachineEntity;//2017-1-11 家用热水机特殊属性 ,会在机型提示中使用到
	
	/**
	 * // 热水机： 配套水箱 || 商用热水机：水箱大小
	 */
	private String matchWaterbox ="";
	/**
	 * // 多联机：内机到外机连接管长度；热水机：水箱安装位置 ||商用热水机：机组安装位置
	 */
	private String ioConnectionLength;
	/**
	 * // 多联机：内机与外机的最大落差；热水机：冷媒连接管||商用热水机：机组与水箱落差
	 */
	private String dropHeight;
	/**
	 * // 多联机：内机与内机的最大落差；热水机：使用水源;商用热水机：使用水源
	 */
	private String inDropHeight;
	/**
	 * 商用热水机：是否安装回水装置// 多联机：外机最大风管长度；热水机：使用人数
	 */
	private String tubeLength;
	/**
	 * // 多联机：冷媒追加量；热水机：热水用水点;
	 */
	private String addCoolantWeight;
	/**
	 * // 多联机：外机总线径；热水机：户型面积
	 */
	private String diameter;
	/**
	 * // 安装位置
	 */
	private String installationSite;
	
	/**
	 * 条码前5位
	 */
	private String fiveCode;


	/**
	 * 初始化
	 * @param context
	 */
	public JY_HotWaterMachine(Activity context) {
		super(context);
		//390697 屏蔽是为了在机型提示的关联资料中去获取开机密码
//		initView();
		jyHotWaterMachineEntity = new JYHotWaterMachineEntity();
	}
	public String getFiveCode() {
		return fiveCode;
	}
	public void setFiveCode(String fiveCode) {
		this.fiveCode = fiveCode;
	}
	@Override
	public void initView() {
		layout_JY_HotWaterMachine = findViewById(R.id.Layout_JY_HotWaterMachine);
		layout_MachineAttr = findViewById(R.id.layout_MachineAttr);
		
		spinner_water_inlet_sequence = (Spinner) findViewById(R.id.spinner_water_inlet_sequence);
		spinner_MachineAttr = (Spinner) findViewById(R.id.spinner_MachineAttr);
		spinner_water_tank_installation_position = (Spinner) findViewById(R.id.spinner_waterTank_position);
		spinner_refrigerant_connecting_tube = (Spinner) findViewById(R.id.spinner_refrigerant_connecting_tube);
		spinner_use_of_water = (Spinner) findViewById(R.id.spinner_use_of_water);
		spinner_number_of_people_use = (Spinner) findViewById(R.id.spinner_number_of_people_use);
		spinner_hot_water_point = (Spinner) findViewById(R.id.spinner_hot_water_point);
		spinner_apartment_layout_area = (Spinner) findViewById(R.id.spinner_apartment_layout_area);
		dialogTip = findViewById(R.id.dialog_water_inlet_sequence);
		
	}
	public void showView() {
		layout_JY_HotWaterMachine.setVisibility(View.VISIBLE);
		initInstallPosition();
		initConnectingTube();
		initUseWater();
		intUserNum();
		initHotWaterPoint();
		initDepartArea();
		initWaterInletSequence();
	}
	public void showView(String barcode) {
		layout_JY_HotWaterMachine.setVisibility(View.VISIBLE);
		initMachineAttr(barcode);
		initInstallPosition();
		initConnectingTube();
		initUseWater();
		intUserNum();
		initHotWaterPoint();
		initDepartArea();
		initWaterInletSequence();
	}
	
	public void hideView(){
		layout_JY_HotWaterMachine.setVisibility(View.GONE);
		matchWaterbox ="";
	}
	
	
	@Override
	public boolean validateView() {
		if (matchWaterbox == null || inDropHeight == null || tubeLength == null || dropHeight == null
				|| ioConnectionLength == null || diameter == null
				|| installationSite == null || addCoolantWeight == null) {
			Utils.showToastCenter(activity,
					getString(R.string.barcode_info_is_incomplete));
			return false;
		}
		return true;
	}
	@Override
	public HctResult getPwdResult(PwdEntity paramEntity,LocationEntity locationEntity, boolean isClounServer) throws Exception {
		HctResult result = new HctResult();
		result = controller.getBootPwd(paramEntity.phone, paramEntity.stripeCode,
				locationEntity.getmGpsLocation() ,
				locationEntity.getmBaseLocation(), locationEntity.getmWifiLocation(),locationEntity.getProvince_loc(),
				locationEntity.getCity_loc(), locationEntity.getProvince(), locationEntity.getCity(), locationEntity.getDistrict(), paramEntity.street,
				paramEntity.projectName, paramEntity.ownerName, paramEntity.ownerPhone, paramEntity.remark,
				paramEntity.industry, ioConnectionLength,
				inDropHeight, dropHeight, tubeLength,
				addCoolantWeight, diameter, matchWaterbox,
				installationSite, "", "",
				locationEntity.getmLatitude() + "",locationEntity.getmLongitude() + "", "", isClounServer);
		return result;
	}

	/**
	 *关联提示页面用
	 * @param paramEntity
	 * @param locationEntity
	 * @param isClounServer
	 * @return
     * @throws Exception
     */
	public HctResult getPwdResult(PwdEntity paramEntity,LocationEntity locationEntity, boolean isClounServer,JYHotWaterMachineEntity jyHotWaterMachineEntity) throws Exception {
		HctResult result = new HctResult();
		result = controller.getBootPwd(paramEntity.phone, paramEntity.stripeCode,
				locationEntity.getmGpsLocation() ,
				locationEntity.getmBaseLocation(), locationEntity.getmWifiLocation(),locationEntity.getProvince_loc(),
				locationEntity.getCity_loc(), locationEntity.getProvince(), locationEntity.getCity(), locationEntity.getDistrict(), paramEntity.street,
				paramEntity.projectName, paramEntity.ownerName, paramEntity.ownerPhone, paramEntity.remark,
				paramEntity.industry, jyHotWaterMachineEntity.getIoConnectionLength(),
				jyHotWaterMachineEntity.getInDropHeight(), jyHotWaterMachineEntity.getDropHeight(), jyHotWaterMachineEntity.getTubeLength(),
				jyHotWaterMachineEntity.getAddCoolantWeight(), jyHotWaterMachineEntity.getDiameter(), jyHotWaterMachineEntity.getMatchWaterbox(),
				jyHotWaterMachineEntity.getInstallationSite(), "", "",
				locationEntity.getmLatitude() + "",locationEntity.getmLongitude() + "", "", isClounServer);
		return result;
	}

	
	/**
	 * 热水机配套水箱
	 * 
	 * @param barcode
	 */
	public void initMachineAttr(final String barcode) {

		final Handler handler = new Handler(getMainLooper()) {
			@Override
			public void dispatchMessage(Message msg) {
				switch (msg.what) {
				case HctConstants.STATUS_FAILD:
					matchWaterbox = "";
					jyHotWaterMachineEntity.setMatchWaterbox(matchWaterbox);
					layout_MachineAttr.setVisibility(View.GONE);
					break;
				case HctConstants.STATUS_SUCCESS:
					HctResult hctResult = (HctResult) msg.obj;
					if (hctResult != null
							&& hctResult.status == HctConstants.STATUS_SUCCESS) {
						final List<AttrEntity> items = (List<AttrEntity>) hctResult.data;

						SpinnerAttrsAdapter adapter = new SpinnerAttrsAdapter(
								activity, items);
						spinner_MachineAttr.setAdapter(adapter);
						layout_MachineAttr.setVisibility(View.VISIBLE);
						matchWaterbox = items.get(0).id;
						jyHotWaterMachineEntity.setMatchWaterbox(matchWaterbox);

						spinner_MachineAttr.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								matchWaterbox = items.get(arg2).id;
								jyHotWaterMachineEntity.setMatchWaterbox(matchWaterbox);
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {

							}
						});

					} else {
						matchWaterbox = "";
						jyHotWaterMachineEntity.setMatchWaterbox(matchWaterbox);
						layout_MachineAttr.setVisibility(View.GONE);
					}
					break;
				}
				super.dispatchMessage(msg);
			}
		};

		new Thread() {
			public void run() {
				HctController controller = new HctController(
						activity);
				try {
					HctResult hctResult = controller.getMachineAttr(
							getApplicationContext(), barcode.substring(0, 5));
					handler.sendMessage(handler.obtainMessage(
							HctConstants.STATUS_SUCCESS, hctResult));
				} catch (Exception e) {
					handler.sendEmptyMessage(HctConstants.STATUS_FAILD);
					e.printStackTrace();
				}
			};
		}.start();

	}
	/**
	 * 热水机水箱安装位置
	 */
	private void initInstallPosition() {
		
		final String[] items = getResources().getStringArray(
				R.array.water_tank_installation_position);
		ioConnectionLength = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_water_tank_installation_position.setAdapter(adapter);

		spinner_water_tank_installation_position.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					ioConnectionLength = null;
				} else {
					ioConnectionLength = items[arg2];
				}
				jyHotWaterMachineEntity.setIoConnectionLength(ioConnectionLength);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	/**
	 * 热水机冷媒连接管
	 */
	private void initConnectingTube() {
		final String[] items = getResources().getStringArray(
				R.array.refrigerant_connecting_tube);
		dropHeight = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_refrigerant_connecting_tube.setAdapter(adapter);

		spinner_refrigerant_connecting_tube.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					dropHeight = null;
				} else {
					dropHeight = items[arg2];
				}
				jyHotWaterMachineEntity.setDropHeight(dropHeight);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	/**
	 * 使用水源
	 */
	private void initUseWater() {
		
		final String[] items = getResources().getStringArray(
				R.array.use_of_water);
		inDropHeight = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_use_of_water.setAdapter(adapter);

		spinner_use_of_water.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					inDropHeight = null;
				} else {
					inDropHeight = items[arg2];
				}
				jyHotWaterMachineEntity.setInDropHeight(inDropHeight);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	/**
	 * 使用人数
	 */
	private void intUserNum() {

		final String[] items = getResources().getStringArray(
				R.array.number_of_people_use);
		tubeLength = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_number_of_people_use.setAdapter(adapter);

		spinner_number_of_people_use.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					tubeLength = null;
				} else {
					tubeLength = items[arg2];
				}
				jyHotWaterMachineEntity.setTubeLength(tubeLength);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	/**
	 * 热水机热水用水点
	 */
	private void initHotWaterPoint() {

		final String[] items = getResources().getStringArray(
				R.array.hot_water_point);
		addCoolantWeight = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_hot_water_point.setAdapter(adapter);

		spinner_hot_water_point.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					addCoolantWeight = null;
				} else {
					addCoolantWeight = items[arg2];
				}
				jyHotWaterMachineEntity.setAddCoolantWeight(addCoolantWeight);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	/**
	 * 热水机户型面积
	 */
	private void initDepartArea() {
		
		final String[] items = getResources().getStringArray(
				R.array.apartment_layout_area);
		diameter = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_apartment_layout_area.setAdapter(adapter);

		spinner_apartment_layout_area.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					diameter = null;
				} else {
					diameter = items[arg2];
				}
				jyHotWaterMachineEntity.setDiameter(diameter);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}
	/**
	 * 进水管阀件安装顺序
	 */
	private void initWaterInletSequence() {
		dialogTip.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new Builder(activity);
				builder.setTitle(R.string.dialog_default_title_hint)
						.setMessage(R.string.tip_water_inlet_sequence)
						.setNegativeButton(
								getString(R.string.dialog_confirm_hint),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialogTip.setVisibility(View.GONE);
										spinner_water_inlet_sequence.setVisibility(View.VISIBLE);
									}
								}).create().show();
			}
		});

		final String[] items = getResources().getStringArray(
				R.array.water_inlet_sequence);
		installationSite = null;
		SpinnerStringsAdapter adapter = new SpinnerStringsAdapter(activity, items);
		spinner_water_inlet_sequence.setAdapter(adapter);
		
		spinner_water_inlet_sequence.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					installationSite = null;
				} else {
					installationSite = items[arg2];
				}
				jyHotWaterMachineEntity.setInstallationSite(installationSite);
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		
		
	}
}
